{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 93,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Function for polynomial display\n",
    "# We do not need pay much attention on this part\n",
    "def plot_polynomial(xmin, xmax, coef, color='C1'):\n",
    "    #xs is an array of evenly spaced numbers between xmin and xmax\n",
    "    xs = np.linspace(xmin, xmax, num=500)\n",
    "    \n",
    "    #ys is an array, each element is computed as a polynomial function of\n",
    "    #the corresponding element of xs\n",
    "    ys = np.zeros_like(xs)\n",
    "    for p, c in enumerate(coef.flatten()):\n",
    "        ys += c*np.power(xs, p)\n",
    "    plt.plot(xs, ys, color=color)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "metadata": {},
   "outputs": [],
   "source": [
    "X0 = np.array([[2], [7], [9], [3], [10], [6], [1], [8]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "metadata": {},
   "outputs": [],
   "source": [
    "ones = np.ones_like(X0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "metadata": {},
   "outputs": [],
   "source": [
    "X = np.concatenate((X0, ones), axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 2,  1],\n",
       "       [ 7,  1],\n",
       "       [ 9,  1],\n",
       "       [ 3,  1],\n",
       "       [10,  1],\n",
       "       [ 6,  1],\n",
       "       [ 1,  1],\n",
       "       [ 8,  1]])"
      ]
     },
     "execution_count": 98,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "metadata": {},
   "outputs": [],
   "source": [
    "Y = np.array([[13], [35], [41], [19], [45], [28], [10], [55]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[13],\n",
       "       [35],\n",
       "       [41],\n",
       "       [19],\n",
       "       [45],\n",
       "       [28],\n",
       "       [10],\n",
       "       [55]])"
      ]
     },
     "execution_count": 100,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXQAAAD8CAYAAABn919SAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvhp/UCwAADrtJREFUeJzt3X9onPd9wPH3Z4pKr2mZksUz/tHMgQaVMEg0REjnMbqkmUJbGv1RQmEr/iPgf8qWbkVd9NcYjOGi0R9/DUzS1bAsP0hVOXRQNTgppTCyyVFWJXFFsizpfHZidY3WbBybon32hx7FVmL37iydnvNX7xcY3X118vPhwG+fnue5eyIzkSRd+X6l7gEkSVvDoEtSIQy6JBXCoEtSIQy6JBXCoEtSIQy6JBXCoEtSIQy6JBXiqu3c2HXXXZcHDhzYzk1K0hXv5MmTP8vMXe0et61BP3DgAHNzc9u5SUm64kXEa508zl0uklQIgy5JhTDoklQIgy5JhTDoklSIbT3LRVL5ZuabTM0ucma5xd6hBhNjw4yP7Kt7rB3BoEvaMjPzTSanF2itrALQXG4xOb0AYNS3gbtcJG2ZqdnFd2K+rrWyytTsYk0T7SwGXdKWObPc6mpdW8ugS9oye4caXa1raxl0SVtmYmyYxuDAhrXG4AATY8M1TbSzeFBU0pZZP/DpWS71MOiSttT4yD4DXhN3uUhSIQy6JBXCoEtSIQy6JBXCoEtSIQy6JBXCoEtSIQy6JBXCoEtSIQy6JBXCoEtSIQy6JBXCoEtSIQy6JBWio4/PjYhXgbeAVeDtzByNiG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U+sYfAQ9FxI+BW4C/qnmeWlS/pTwOPAsssNaYHfWO0Yh4GPhHYDgiTkfEvcAR4M6IeIm132KO9GTbvlNUksrgK3RJKoRBl6RCGHRJKoRBl6RCGHRJKoRBl6RCGHRJKoRBl6RC/D/AeQEbtcuAiQAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.scatter(X0,Y); plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Normal Equation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "metadata": {},
   "outputs": [],
   "source": [
    "theta = np.linalg.inv(X.T.dot(X)).dot(X.T.dot(Y))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[4.40880503],\n",
       "       [5.39937107]])"
      ]
     },
     "execution_count": 103,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "theta"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXQAAAD8CAYAAABn919SAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvhp/UCwAAGcdJREFUeJzt3X90VPWd//HnmxB1Ci1phbIETLHVE+uPamy0KK1rpYo/UGjX0m53abbL+fLdCla7Ngqt+3Vb3dWa77fFH9WKaMVdW3VpBKQeg0WoK1oUiadBMW3FuGVAoEAW1wYJ4f39404sCQmZJHPnzr3zepzDYeZmhvseIC8un7n3NebuiIhI/A2JegAREckNBbqISEIo0EVEEkKBLiKSEAp0EZGEUKCLiCSEAl1EJCEU6CIi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},
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# See how it fits:\n",
    "plt.scatter(X0, Y)\n",
    "plot_polynomial(0, 10, theta)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Gradient Descent"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "metadata": {},
   "outputs": [],
   "source": [
    "m = X.shape[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8"
      ]
     },
     "execution_count": 106,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "m"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "metadata": {},
   "outputs": [],
   "source": [
    "theta_gd = np.random.normal(size=2).reshape([2,1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-1.45366214],\n",
       "       [ 1.58188137]])"
      ]
     },
     "execution_count": 108,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "theta_gd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "metadata": {},
   "outputs": [],
   "source": [
    "learning_rate = 0.02"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "metadata": {},
   "outputs": [],
   "source": [
    "def grad_cal(X, Y, theta_gd, m):\n",
    "    \"\"\"\n",
    "    X: X's value\n",
    "    Y: Y's value\n",
    "    theta_gd: theta's value\n",
    "    m: number of samples\n",
    "    \"\"\"    \n",
    "    g = 1/m * X.T.dot(X.dot(theta_gd) - Y)\n",
    "    return g.reshape(theta_gd.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "metadata": {},
   "outputs": [],
   "source": [
    "def loss(X, Y, theta_gd, m):\n",
    "    \"\"\"\n",
    "    X: X's value\n",
    "    Y: Y's value\n",
    "    theta_gd: theta's value\n",
    "    m: number of samples\n",
    "    \"\"\"\n",
    "    return 1/(2*m) * np.sum((X.dot(theta_gd) - Y)**2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 120,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "16.263364779874216\n",
      "16.263364779874216\n",
      "16.263364779874216\n",
      "16.263364779874216\n",
      "16.263364779874216\n",
      "16.263364779874216\n",
      "16.263364779874216\n",
      "16.263364779874216\n",
      "16.263364779874216\n",
      "16.263364779874216\n"
     ]
    }
   ],
   "source": [
    "for i in range(10000):\n",
    "    grad_value = grad_cal(X, Y, theta_gd, m)\n",
    "    theta_gd = theta_gd - learning_rate*grad_value\n",
    "    if (i+1)%1000 == 0:\n",
    "        print(loss(X, Y, theta_gd, m))\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 121,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[4.40880503]\n",
      " [5.39937107]]\n"
     ]
    }
   ],
   "source": [
    "print(theta_gd)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXQAAAD8CAYAAABn919SAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvhp/UCwAAGcdJREFUeJzt3X90VPWd//HnmxB1Ci1phbIETLHVE+uPamy0KK1rpYo/UGjX0m53abbL+fLdCla7Ngqt+3Vb3dWa77fFH9WKaMVdW3VpBKQeg0WoK1oUiadBMW3FuGVAoEAW1wYJ4f39404sCQmZJHPnzr3zepzDYeZmhvseIC8un7n3NebuiIhI/A2JegAREckNBbqISEIo0EVEEkKBLiKSEAp0EZGEUKCLiCSEAl1EJCEU6C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
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# See how it fits:\n",
    "plt.scatter(X0, Y)\n",
    "plot_polynomial(0, 10, theta_gd)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Predict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 123,
   "metadata": {},
   "outputs": [],
   "source": [
    "def predict(X):\n",
    "    return X.dot(theta)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 130,
   "metadata": {},
   "outputs": [],
   "source": [
    "X_new_1 = np.array([[20, 1]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 131,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[93.5754717]])"
      ]
     },
     "execution_count": 131,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "predict(X_new_1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 132,
   "metadata": {},
   "outputs": [],
   "source": [
    "X_new_2 = np.array([[40, 1]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 133,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[181.75157233]])"
      ]
     },
     "execution_count": 133,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "predict(X_new_2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
